<template>
    <!-- 查询区域 -->
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div>

      <a-table
        style="overflow: scroll;height: 40vh"
        ref="table"
        size="middle"
        bordered
        rowKey="recId"
        class="j-table-force-nowrap"
        :scroll="{x: true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
        :customRow="clickThenSelect"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">导出</a>
        </span>
      </a-table>
    </div>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import '@assets/less/TableExpand.less'
import { getAction } from '@api/manage'

export default {
  name: 'MesUnitHistoryList',
  mixins: [JeecgListMixin],
  components: {},
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    },
    line: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['unitsn'] = val
          this.queryParam['line'] = this.line
          this.loadData(1)
        }
      }
    },
    line: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['unitsn'] = this.mainId
          this.queryParam['line'] = this.line
          this.loadData(1)
        }
      }
    }
  },
  data() {
    return {
      description: '序列号列表管理页面',
      disableMixinCreated: true,
      // 表头
      columns: [
        {
          title: '序列号',
          align: 'center',
          dataIndex: 'unitsn'
        },
        {
          title: '工站',
          align: 'center',
          dataIndex: 'stn'
        },
        {
          title: '结果',
          align: 'center',
          dataIndex: 'result_dictText'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'cycleType',
          customRender: function(text) {
            if (text === '2') {
              return '返工'
            } else {
              return '加工'
            }
          }
        },
        {
          title: '加工开始时间',
          align: 'center',
          dataIndex: 'startdt'
        },
        {
          title: '加工结束时间',
          align: 'center',
          dataIndex: 'enddt'
        },
        {
          title: '操作工',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '班次日期',
          align: 'center',
          dataIndex: 'shiftDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '班次',
          align: 'center',
          dataIndex: 'shift'
        },
        {
          title: '产线类型',
          align: 'center',
          dataIndex: 'segmentCode_dictText'
        }
      ],
      url: {
        list: '/trace/barcodeTrace/listMesUnitHistoryByUnitsn'
      },
      dictOptions: {},
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 50,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl() {
      return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    initDictConfig() {
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.recId.split(','), [record])
            this.$emit('itemclick', record)
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },

    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams()// 查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'recId', text: '主键ID', dictCode: '' })
      fieldList.push({ type: 'string', value: 'unitsn', text: '序列号', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'runtimeid', text: '运行时间ID', dictCode: '' })
      fieldList.push({ type: 'string', value: 'partno', text: '产品或产品组', dictCode: '' })
      fieldList.push({ type: 'string', value: 'terminal', text: '终端', dictCode: '' })
      fieldList.push({ type: 'string', value: 'line', text: '产线或产线组', dictCode: '' })
      fieldList.push({ type: 'string', value: 'op', text: '工位', dictCode: '' })
      fieldList.push({ type: 'string', value: 'stn', text: '工站', dictCode: '' })
      fieldList.push({ type: 'string', value: 'operator', text: '操作工', dictCode: '' })
      fieldList.push({
        type: 'string',
        value: 'cycleType',
        text: '0=Normal; 1=Rework without rework claim;  2=Rework with rework claim',
        dictCode: ''
      })
      fieldList.push({
        type: 'string',
        value: 'runType',
        text: '0=Normal (with load and unload)  1=Pass  through',
        dictCode: ''
      })
      fieldList.push({ type: 'date', value: 'startdt', text: '加工开始时间' })
      fieldList.push({ type: 'date', value: 'enddt', text: '加工开始时间' })
      fieldList.push({ type: 'string', value: 'rwkCode', text: '返工代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'result', text: '0=NOK, 1=OK,2=NA', dictCode: '' })
      fieldList.push({ type: 'date', value: 'shiftDate', text: '班次日期' })
      fieldList.push({ type: 'string', value: 'shift', text: '班次', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'parttypeid', text: '产品ID', dictCode: '' })
      fieldList.push({ type: 'string', value: 'segmentCode', text: '0:主线 1:1号支线 2:2号支线', dictCode: '' })
      fieldList.push({ type: 'string', value: 'woid', text: '工单ID', dictCode: '' })
      fieldList.push({ type: 'date', value: 'ctime', text: '更新时间' })
      this.superFieldList = fieldList
    }

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
